package com.example.administrator.test;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;

import java.util.Calendar;
import java.util.TimeZone;


/**
 * Created by Administrator on 2017/8/31.
 */

public class FourFragment extends Fragment implements View.OnClickListener {

    private MyApplication app1;
    DbManager db;
    private MyApplication app2;

    TextView account;

    TextView time;

    LinearLayout phone;
    LinearLayout web;

    View view;
    @SuppressLint("WrongConstant")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_four,null);


        time = (TextView)view.findViewById(R.id.tab4_textview_time);
        new MyThread().start();
        initView();
        return view;
    }

    private void initView() {
        app2 = (MyApplication)getActivity().getApplication();
        account = (TextView)view.findViewById(R.id.tab4_account);
        account.setText("用户名:"+app2.getAccount());

        phone = (LinearLayout)view.findViewById(R.id.tab4_phone);
        phone.setOnClickListener(this);
        web = (LinearLayout)view.findViewById(R.id.tab4_web);
        web.setOnClickListener(this);
    }

    @SuppressLint("WrongConstant")
    public  String getTime()
    {
        //创建日历对象
        Calendar calendar = Calendar.getInstance();
        //设置时区
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        String month = String.valueOf(calendar.get(Calendar.MONTH)+1);
        String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
        String minutes = String.valueOf(calendar.get(Calendar.MINUTE));
        String second = String.valueOf(calendar.get(Calendar.SECOND));
        String week = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        if("1".equals(week)){
            week="星期天";
        }else if("2".equals(week)){
            week = "星期一";
        }else if("3".equals(week)){
            week = "星期二";
        }else if("4".equals(week)){
            week = "星期三";
        }else if("5".equals(week)){
            week = "星期四";
        }else if("6".equals(week)){
            week = "星期五";
        }else if("7".equals(week)){
            week = "星期六";
        }
        return year+":"+month+":"+day+":"+hour+":"+minutes+":"+second+":"+week;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tab4_phone:
                Intent phone = new Intent();
                phone.setAction(Intent.ACTION_CALL);
                phone.setData(Uri.parse("tel:1380013800"));
                startActivity(phone);
                break;
            case R.id.tab4_web:
                Intent web = new Intent(getActivity(),WebActivity.class);
                startActivity(web);
                break;
        }
    }

    class MyThread extends Thread{
        @Override
        public void run() {
            do{
                Message message = new Message();
                message.what = 0;
                handler.sendMessage(message);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }while (true);
        }
    }

    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                case 0:
                    time.setText(getTime());
                    break;
            }
        }
    };

}
